import { StudentService } from '@/services/student';
import { ActionType } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { message } from 'antd';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

export default () => {
  const { qualification } = useModel('dic');
  const qualificationEnum = useMemo(() => {
    const result = new Map();
    result.set(0, { text: '- 请选择 -' });
    qualification.forEach((item) =>
      result.set(item.dic_id, { text: item.dic_name }),
    );
    return result;
  }, [qualification]);

  const actionRef = useRef<ActionType>();
  const getHandler = useCallback(async (body: API.Student_Query_Body) => {
    return StudentService.queryStudentList(body);
  }, []);

  const addHandler = useCallback(async (body: API.Student_Add_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await StudentService.addStudent(body);
      hide();
      message.success(`新增成功！`);
      actionRef.current?.reload?.();
      return true;
    } catch (error) {
      hide();
      message.error(`新增失败，请重试！`);
      return false;
    }
  }, []);

  const updateHandler = useCallback(async (body: API.Student_Update_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await StudentService.updateStudent(body);
      hide();
      message.success(`修改成功！`);
      actionRef.current?.reload?.();
      return true;
    } catch (error) {
      hide();
      message.error(`修改失败，请重试！`);
      return false;
    }
  }, []);

  const assignHandler = useCallback(async (body: API.Student_Assign_Body) => {
    const hide = message.loading('dismissing..');
    try {
      await StudentService.assignStudent(body);
      hide();
      message.success(`分班成功！`);
      actionRef.current?.reload?.();
      return true;
    } catch (error) {
      hide();
      message.error(`分班失败，请重试！`);
      return false;
    }
  }, []);
  const avateHandler = useCallback(
    async (body: API.Student_AvatarUpdate_Body) => {
      const hide = message.loading('dismissing..');
      try {
        await StudentService.avateUpdate(body);
        hide();
        message.success(`图片更新成功！`);
        actionRef.current?.reload?.();
        return true;
      } catch (error) {
        hide();
        message.error(`图片更新失败，请重试！`);
        return false;
      }
    },
    [],
  );
  const avateLoadHandler = useCallback(
    async (avatar:FormData) => {
      const hide = message.loading('dismissing..');
      try {
        await StudentService.avateUpdateload(avatar);
        hide();
        message.success(`图片上传成功！`);
        actionRef.current?.reload?.();
        return true;
      } catch (error) {
        hide();
        message.error(`图片上传失败，请重试！`);
        return false;
      }
    },
    [],
  );

  const [classList, setClassList] = useState<any>([]);
  useEffect(() => {
    (async () => {
      const data = await StudentService.classList();
      setClassList(data);
    })();
  }, []);
  const classCategoryEnum = useMemo(() => {
    const result = new Map();
    result.set(0, { text: '- 请选择 -' });
    classList.forEach((item: API.ClassInfo) =>
      result.set(item.cls_id, { text: item.cls_name }),
    );
    return result;
  }, [classList]);

  return {
    qualificationEnum,
    classCategoryEnum,
    qualification,
    actionRef,
    getHandler,
    addHandler,
    updateHandler,
    assignHandler,
    avateHandler,
    avateLoadHandler,
  };
};
